<?php
function mi_publica_settings_page_content() {
    ?>
    <style>
        .form-table input{
            min-width: 800px;
        }

    </style>
    <div class="wrap">
        <h1>Settings</h1>
        <form method="post" action="options.php">
            <?php
            settings_fields('mi_publica_settings');
            do_settings_sections('mi_publica_settings');
            submit_button();
            ?>
        </form>
    </div>
    <?php
}

function mi_publica_register_settings() {
    register_setting('mi_publica_settings', 'mi_publica_product_id');
    register_setting('mi_publica_settings', 'mi_publica_api_link');
    //register_setting('mi_publica_settings', 'mi_publica_api_key');
    register_setting('mi_publica_settings', 'mi_publica_design_link');
    register_setting('mi_publica_settings', 'mi_publica_link_text');
    register_setting('mi_publica_settings', 'mi_publica_download_link');
}
add_action('admin_init', 'mi_publica_register_settings');


function mi_publica_settings_fields() {
    add_settings_section(
        'mi_publica_general_section',
        '参数设置',
        'mi_publica_general_section_callback',
        'mi_publica_settings'
    );
    add_settings_field(
        'mi_publica_design_link',
        '设计系统入口链接',
        'mi_publica_design_link_callback',
        'mi_publica_settings',
        'mi_publica_general_section'
    );

    add_settings_field(
        'mi_publica_link_text',
        '按钮文字',
        'mi_publica_link_text_callback',
        'mi_publica_settings',
        'mi_publica_general_section'
    );

    add_settings_field(
        'mi_publica_product_id',
        '绑定的商品ID',
        'mi_publica_product_id_callback',
        'mi_publica_settings',
        'mi_publica_general_section'
    );

    add_settings_field(
        'mi_publica_api_link',
        '接口链接',
        'mi_publica_api_link_callback',
        'mi_publica_settings',
        'mi_publica_general_section'
    );

    add_settings_field(
        'mi_publica_download_link',
        '下载接口',
        'mi_publica_download_link_callback',
        'mi_publica_settings',
        'mi_publica_general_section'
    );


    // add_settings_field(
    //     'mi_publica_api_key',
    //     '接口Key',
    //     'mi_publica_api_key_callback',
    //     'mi_publica_settings',
    //     'mi_publica_general_section'
    // );
}
add_action('admin_init', 'mi_publica_settings_fields');


function mi_publica_general_section_callback() {
    echo '短代码：[design_link_button]';
}

function mi_publica_design_link_callback() {
    $design_link = get_option('mi_publica_design_link');
    echo '<input type="text" name="mi_publica_design_link" value="' . esc_attr($design_link) . '" />';
}

function mi_publica_link_text_callback() {
    $design_link_text = get_option('mi_publica_link_text');
    echo '<input type="text" name="mi_publica_link_text" value="' . esc_attr($design_link_text) . '" />';
}


function mi_publica_product_id_callback() {
    $product_id = get_option('mi_publica_product_id');
    echo '<input type="text" name="mi_publica_product_id" value="' . esc_attr($product_id) . '" />';
}

function mi_publica_api_link_callback() {
    //$api_link = get_option('mi_publica_api_link');
    $api_namespace = 'mi-publica/v1';
    $api_route = '/design';
    $api_link = rest_url($api_namespace . $api_route);
    echo '<input type="text" name="mi_publica_api_link" value="' . esc_attr($api_link) . '?design_img=图片链接&design_color=ffffff&design_id=123456" disabled/>';
}

function mi_publica_download_link_callback() {
    $download_link = get_option('mi_publica_download_link');
    echo '<input type="text" name="mi_publica_download_link" value="' . esc_attr($download_link) . '" />';
    echo '<p>download link like this: '.esc_attr($download_link).'?artNo=abcdefg&orderNo=123</p>';
}

// function mi_publica_api_key_callback() {
//     $api_key = get_option('mi_publica_api_key');
//     //echo '<input type="text" name="mi_publica_api_key" value="' . esc_attr($api_key) . '" />';
// }
